package studio.raptor.dispatcher.server.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import studio.raptor.dispatcher.scheduler.zookeeper.ZookeeperConfiguration;
import studio.raptor.gid.Sequencer;
import studio.raptor.gid.def.BreadcrumbDef;

/**
 * @author jack
 * @since 1.0
 */
@Configuration
public class SequencesConfig {

  @Autowired
  ZookeeperConfiguration zkConfig;


  @Bean(destroyMethod = "shutdown")
  @ConditionalOnBean(ZookeeperConfiguration.class)
  public Sequencer sequencer() throws Exception {
    Sequencer server = new Sequencer(zkConfig.getServers(), zkConfig.getNamespace(), "");
    server.startup();
    server.add(new BreadcrumbDef() {

      //序列起始值
      @Override
      public long start() {
        return 100000000000000000l;
      }

      //序列名称
      @Override
      public String name() {
        return "traceId";
      }

      //步长
      @Override
      public long incr() {
        return 1;
      }

      @Override
      public int cache() {
        return 1000;
      }
    });

    return server;
  }

}
